package com.rpay.admin.config.system;

import com.rpay.admin.config.jwt.JsonWebTokenSecurityConfig;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@Configurable
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends JsonWebTokenSecurityConfig {

   /*@Autowired
    private MyAuthenticationProvider provider;//自定义验证*/

/*    @Autowired
    private UserDetailsService userDetailsService;//自定义用户服务

    @Autowired
    private MySecurityFilter mySecurityFilter;
    @Autowired
    private MyUserDetailsService customUserDetailsService;

    @Autowired
    private CustomAccessDecisionManager accessDecisionManager;*/

   /* @Autowired
    private MyAuthenticationProvider authenticationProvider;*/


    @Override
    protected void setupAuthorization(HttpSecurity http) throws Exception {
        // http.authorizeRequests().accessDecisionManager(null).and().authenticationProvider(null).sessionManagement();
        //静态文件不做处理，因为我们目前做的是动静分离，所有动态请求才会走到后端来

               /* .antMatchers(StaticParams.PATHREGX.AUTHADMIN).hasAuthority(StaticParams.USERROLE.ROLE_ADMIN)//admin角色访问权限

                .antMatchers(StaticParams.PATHREGX.AUTHUSER).hasAuthority(StaticParams.USERROLE.ROLE_USER)//user角色访问权限*/

                /*.anyRequest()//all others request authentication
                .authenticated()
                .and()
                .formLogin().loginPage("/login").permitAll()
                .and()
                .logout().permitAll();*/


        /*http
                //在正确的位置添加我们自定义的过滤器
                .authorizeRequests().accessDecisionManager(accessDecisionManager).and().authenticationProvider(authenticationProvider)
                .addFilterBefore(mySecurityFilter, FilterSecurityInterceptor.class).csrf().disable().authorizeRequests()
                .antMatchers("/noa").permitAll() //访问noa资源不需要权限
                .anyRequest().authenticated()
                //.antMatchers("/hello").hasAuthority("ADMIN")
                .and()
                .formLogin().loginProcessingUrl("/noa/login").usernameParameter("username").passwordParameter("password")
                .permitAll()
                .successHandler(loginSuccessHandler())//code3
                .failureHandler(authenticationFailureHandler())
                .and()
                .logout()
                .logoutSuccessUrl("/noa")
                .permitAll()
                .invalidateHttpSession(true)
                .and()
                .rememberMe()
                .tokenValiditySeconds(1209600)
                .tokenRepository(tokenRepository());*/
        http.authorizeRequests()
                // allow anonymous access to /user/login endpoint
                .antMatchers("/noa*//**").permitAll()
                // 测试环境使用
//                .antMatchers("/api*//**").permitAll()
                .antMatchers("/health*//**").permitAll()
                .antMatchers("/metrics*//**").permitAll()
                .antMatchers("/trace*//**").permitAll()
                .antMatchers("/dump*//**").permitAll()
                //.antMatchers(HttpMethod.OPTIONS,"/noa/**","/api/**").permitAll()
                // authenticate all other requests
                .anyRequest().authenticated();
    }


   /* @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        //指定密码加密所使用的加密器为passwordEncoder()
        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
        //不删除凭据，以便记住用户
        auth.eraseCredentials(false);
    }*/


/*    @Bean
    public MessageDigestPasswordEncoder passwordEncoder() {
        return new ShaPasswordEncoder(512);
    }

    @Bean
    public LoginSuccessHandler loginSuccessHandler() {
        return new LoginSuccessHandler();
    }

    @Bean
    public SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler() {
        return new SimpleUrlAuthenticationSuccessHandler();
    }

    public JdbcTokenRepositoryImpl tokenRepository() {
        JdbcTokenRepositoryImpl j = new JdbcTokenRepositoryImpl();
        j.setDataSource(dataSource);
        return j;
    }*/
}